﻿/*!
* Chef Framework v0.1, http://chef.codeplex.com
* Copyright 2010, I-Technology.NET, http://i-technology.net
* Distributed in whole under the terms of the MIT license
* http://www.opensource.org/licenses/mit-license.php 
* 
* Author
* Robert Hoffmann
*/

(function(window, $){
    function Chef(){
        this.traceLevel = {
            Info    :1,
            Warning :2,
            Error   :4
        };

        this.hash                = window.location.hash;
        this.urlMonitor          = null;
        this.urlMonitorFrequency = 250;    
    }
        
    Chef.prototype = {
        start: function(level){
            this.traceOptions = level;			
            this.urlMonitor   = setInterval("chef.monitorUrl()", this.urlMonitorFrequency);
        },
        
        stop: function(){
            clearInterval(this.urlMonitor);
        },

        log: function(level){
            if ((this.traceOptions & level == level) && window.console)
                console.log(arguments);
        },

        replace: function(target, obj){
            target = obj;
        },
        
        monitorUrl: function(){
            this.log(this.traceLevel.Info,"running", new Date());
            
            if (window.location.hash != this.hash) {
                this.hash = window.location.hash;

                // info:"triggered" is injected when event is raised
                $(this).triggerHandler(window.location.hash, {info:"triggered"});
            }
        },

        mapRoute: function(route, handler) {
            // split route into route/data ?
            // #path/function/:data

            // then figure out how to bind route correctly & extract data from it
            // json data, string, int's ....

            // info:"bound" is set, one time, when event is first bound
            $(this).bind(route, {info:"bound"}, handler);
        }
    };

    window.chef = new Chef();
})(window, jQuery);